package com.zq.solution.treenode;

/**
 * @ClassName IsBalanced
 * @Description 55 - II. 平衡二叉树
 * 输入一棵二叉树的根节点，判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超过1，那么它就是一棵平衡二叉树。
 * @Author ZQ
 * @Date 2020/11/3 17:33
 * @Version V1.0
 **/
public class IsBalanced {
    public boolean isBalanced(TreeNode<Integer> root) {
        if (root == null) return true;
        if (!isBalanced(root.left)) return false;
        if (!isBalanced(root.right)) return false;
        int left = getTreeDeep(root.left);
        int right = getTreeDeep(root.right);
        if (Math.abs(left - right) > 1) return false;
        return true;
    }
    public static int getTreeDeep(TreeNode<Integer> root){
        if (root == null) return 0;
        int left = getTreeDeep(root.left) + 1;
        int right = getTreeDeep(root.right) + 1;

        return Math.max(left,right);
    }
}
